import React from 'react';
import {Button, Checkbox, message, Select, Radio, Icon, Row, Col } from 'antd';
import {
    WeaDialog, WeaSearchGroup, WeaFormItem, WeaInput, WeaSelect, WeaBrowser,
    WeaCheckbox, WeaHelpfulTip, WeaColorPicker, WeaTools
} from 'ecCom';
import {WeaLocaleProvider} from 'ecCom';
import {WeaMaterialLib} from 'weaPortalPublic';
import {action} from "mobx";
const getLabel = WeaLocaleProvider.getLabel;
// 快捷入口元素，数据来源
class Content_QuickEntry_DataSource extends React.Component {

    state = {
        id: this.props.tabData.id,
        title: this.props.tabData.title || '',
        picture: this.props.tabData.picture || '',
        usertype: this.props.tabData.usertype,
        link: this.props.tabData.link || '',
        shareId: this.props.tabData.shareId,
        isLimit: this.props.tabData.isLimit,
        backGroundColor: this.props.tabData.backGroundColor || '#FFFFFF',
        linkMode: this.props.tabData.linkMode && this.props.tabData.linkMode == 'advance2' ? 'advance2' : 'normal',
        replaceDatas: [],
        activeKeystr :'',
        dropMenuUrlstr:'',
        flowidsstring:'',
        dropMenuValuestr:'',
        flowidsstringarray:[],
        flowidsstr : '',
        typeidsstr:'',
        names : "",

    };

    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.onOk = this.onOk.bind(this);
        this.onCancel = this.onCancel.bind(this);
        // this.getMenuNameByMenuid = this.getMenuNameByMenuid.bind(this);
    }

    componentWillMount() {
        const {link, linkMode} = this.props.tabData || '';

            let strs = [];
            let jsonstrstr = "";
            let viewTypestr  = "";
            let dropMenuValuestr  = "";
            let activeKeystr  = "1";
            let dropMenuUrlstr  = "";
            let flowidsstr  = "";
            let typeidsstr  = "";
            let  flowidsstring = "";
            let repaledate1 = [];

            if( ( linkMode == 'advance2')){//3表示选择系统页面
                if(link.indexOf("?")!=-1){
                    let  fullrouteurlarray = link.split("?");
                    let _fullrouteurl = fullrouteurlarray[0];
                    let fullrouteurlparams = fullrouteurlarray[1];
                    let flowidsstringarray = {};
                    if(fullrouteurlparams.indexOf("secid=")!=-1&&_fullrouteurl.indexOf("add")!=-1){//新建文档
                        let  flowidsstrarray = link.split("secid=");
                         dropMenuValuestr = '1';
                         activeKeystr = '1';
                         dropMenuUrlstr = '/spa/document/static/index.html#/main/document/add?secid=';
                         flowidsstring = flowidsstrarray[1];

                    }else if(fullrouteurlparams.indexOf("viewcondition=0&secid=")!=-1&&_fullrouteurl.indexOf("search")!=-1){//我的文档
                        let  flowidsstrarray = link.split("secid=");
                        dropMenuValuestr = '2';
                        activeKeystr = '1';
                        dropMenuUrlstr = '/spa/document/static/index.html#/main/document/search?viewcondition=0&secid=';
                        flowidsstring = flowidsstrarray[1];
                    }else if(fullrouteurlparams.indexOf("viewcondition=1&secid=")!=-1&&_fullrouteurl.indexOf("search")!=-1){//最新文档
                        let  flowidsstrarray = link.split("secid=");
                        dropMenuValuestr = '3'
                        activeKeystr = '1';
                        dropMenuUrlstr = '/spa/document/static/index.html#/main/document/search?viewcondition=1&secid=';
                        flowidsstring = flowidsstrarray[1];
                    }else if(fullrouteurlparams.indexOf("viewcondition=2&secid=")!=-1&&_fullrouteurl.indexOf("search")!=-1){//文档目录
                        let  flowidsstrarray = link.split("secid=");
                        dropMenuValuestr = '4';
                        activeKeystr = '1';
                        dropMenuUrlstr = '/spa/document/static/index.html#/main/document/search?viewcondition=2&secid=';
                        flowidsstring = flowidsstrarray[1];
                    } else if(fullrouteurlparams.indexOf("wfRange=")!=-1){//流程新建
                        let flowidsstrarray = link.split("wfRange=");
                        flowidsstring = flowidsstrarray[1];
                        dropMenuValuestr = '1';
                        activeKeystr = '2';
                        dropMenuUrlstr = '/spa/workflow/static/index.html#/main/workflow/add?';
                        flowidsstring = flowidsstrarray[1];
                        flowidsstringarray = flowidsstring.split(",");
                    }else if(fullrouteurlparams.indexOf("fromwhere=jsonFilter&jsonstr=")!=-1 && _fullrouteurl.indexOf("/main/workflow/supervise")!=-1){//督办流程
                        strs = link.split("fromwhere=jsonFilter&jsonstr=");
                        jsonstrstr = unescape(strs[1]);
                        viewTypestr =  JSON.parse(jsonstrstr).viewType;
                        dropMenuValuestr = '6';
                        activeKeystr = '2';
                        typeidsstr = JSON.parse(jsonstrstr).typeids;
                        flowidsstr = JSON.parse(jsonstrstr).flowids;
                        dropMenuUrlstr = '/spa/workflow/static/index.html#/main/workflow/supervise?fromwhere=jsonFilter&jsonstr=';
                    } else {//其他流程
                        strs = link.split("fromwhere=jsonFilter&jsonstr=");
                        jsonstrstr = unescape(strs[1]);
                        viewTypestr =  JSON.parse(jsonstrstr).viewType;
                        activeKeystr = '2',
                        typeidsstr  =  JSON.parse(jsonstrstr).typeids;
                        flowidsstr = JSON.parse(jsonstrstr).flowids;
                        dropMenuUrlstr = '/spa/workflow/static/index.html#/main/workflow/queryFlowResult?fromwhere=jsonFilter&jsonstr=';
                        if(viewTypestr=='1'){
                                dropMenuValuestr = '2';
                        }else if(viewTypestr=='2'){
                                dropMenuValuestr = '3';
                        }else if(viewTypestr=='3'){
                                dropMenuValuestr = '4';
                        }else if(viewTypestr=='4'){
                                dropMenuValuestr = '7';
                        }else if(viewTypestr=='5'){
                                dropMenuValuestr = '5';
                        }
                    }
                }else{
                    dropMenuValuestr = '';
                    activeKeystr = '';
                    dropMenuUrlstr = '';
                    flowidsstring = '';
                    flowidsstr = "";
                }
            }

            this.getMenuNameByMenuid(dropMenuValuestr,activeKeystr,flowidsstr,flowidsstring,dropMenuUrlstr);
        }

    /**
     * 通过id获取name
     * @constructor
     */
    getMenuNameByMenuid(dropMenuValuestr,activeKeystr,flowidsstr,flowidsstring,dropMenuUrlstr) {

        WeaTools.callApi('/api/portal/portalMenuMaintenance/GetMenuNameByMenuid', 'POST', {
            dropMenuUrlstr : dropMenuUrlstr,
            dropMenuValuestr : dropMenuValuestr,
            activeKeystr : activeKeystr,
            flowidsstr : flowidsstr,//流程其他id
            flowidsstring : flowidsstring,//除过流程其他流程id
        }).then(result => {
            if(result.status == '1'){
                this.setState({names:result.names});
                let repaledate1 = [];
                if(activeKeystr == '1'){
                    let flowidsstringarray = flowidsstring.split(",")
                    let namearray = result.names.split(",")
                    for(let i = 0;i<flowidsstringarray.length;i++) {
                        var repaledate = {};
                        repaledate.dropMenuValue = dropMenuValuestr;
                        repaledate.dropMenuUrl = dropMenuUrlstr;
                        repaledate.id = flowidsstringarray[i];
                        repaledate.activeKey = activeKeystr;
                        repaledate.name = namearray[i];
                        repaledate1.push(repaledate);
                    }
                    this.setState({
                        replaceDatas : repaledate1
                    });
                } else if(activeKeystr == '2' && dropMenuValuestr == '1'){
                    let flowidsstringarray = flowidsstring.split(",");
                    let namearray = result.names.split(",");
                    for(let i = 0;i<namearray.length;i++) {
                        var repaledate = {};
                        repaledate.dropMenuValue = dropMenuValuestr;
                        repaledate.dropMenuUrl = dropMenuUrlstr;
                        repaledate.id = "wf_"+flowidsstringarray[i];
                        repaledate.activeKey = activeKeystr;
                        repaledate.name = namearray[i];
                        repaledate1.push(repaledate);
                    }
                    this.setState({
                        replaceDatas : repaledate1
                    });
                } else {
                    let namearray = result.names.split(",");
                    for(let i = 0;i<namearray.length;i++) {
                        var repaledate = {};
                        repaledate.dropMenuValue = dropMenuValuestr;
                        repaledate.dropMenuUrl = dropMenuUrlstr;
                        repaledate.id = "wf_"+flowidsstr[i];
                        repaledate.activeKey = activeKeystr;
                        repaledate.name = namearray[i];
                        repaledate1.push(repaledate);
                    }
                    this.setState({
                        replaceDatas : repaledate1
                    });
                }
            }

        });
    }


    render() {
        const { id, title,  linkMode, picture, link, backGroundColor, replaceDatas} =  this.state;

        const colProps = {
            labelCol: {span: 6},
            wrapperCol: {span: 16}
        };

        //<WeaInput value={title} viewAttr = '3' onChange={value => this.setState({title: value})}/>

        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@gcodbo`}
                className="portal-quickentry-esetting-dialog"
                visible={true}
                title={getLabel(19480, '内容设置')}
                icon="icon-coms-portal"
                iconBgcolor='#1a57a0'
                style={{width: 660, height: 210}}
                hasScroll={true}
                buttons={this.getButtons()}
                onCancel={this.onCancel}
            >
                <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@mrjb2b`} title={getLabel(33396, '设置项')} needTigger={true} showGroup={true}>
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@9cn7m3`} label={getLabel(24986,'标题')} labelCol={{span: 6}} wrapperCol={{span: 16}}>

                        <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@rbm9jy`}  inputType='multilang' isBase64={true}  viewAttr = '3'
                                   value={title} onChange={value => this.setState({title: value})}/>
                    </WeaFormItem>
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1lggvl`} label={getLabel(515770,'快捷入口图标') } labelCol={{span: 6}} wrapperCol={{span: 16}}>
                        <WeaMaterialLib ecId={`${this && this.props && this.props.ecId || ''}_WeaMaterialLib@m2ihoh`}
                            multiCheck={false}
                            value={picture}
                            isShow={true}
                            onChange={value => this.setState({picture: value})}
                        />
                    </WeaFormItem>
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@k7o6et`} label={getLabel(16208, '链接地址')} {...colProps}>
                        <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@q9e3cg`} gutter={8}>
                            <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@497g4z`} span={6}>
                                <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@m8ehkp`}
                                    options={[
                                        {key: 'normal', selected: false, showname: getLabel(385809, '自定义URL')},
                                        {key: 'advance', selected: false, showname: getLabel(33496, '系统菜单')},
                                        {key: 'advance2', selected: false, showname: getLabel(31560, '系统页面')},
                                    ]}
                                    value={linkMode}
                                    onChange={value => this.setState({linkMode: value})}
                                />
                            </Col>
                            <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@fyr9lj`} span={17}>
                                {linkMode == 'normal' ? <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@jzm3u3`} value={link} onChange={value => this.setState({link: value})}/> : ''}
                                {linkMode == 'advance' ? (
                                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@l5mon2`}
                                        title={getLabel(33496, '系统菜单')}
                                        type={'portalmenu'}
                                        tabs={[
                                            {name: getLabel(33675, '前端菜单'), key: '1', dataParams: {sourcetype: '1'}, conditionDataParams: {sourcetype: '1'}},
                                            {name: getLabel(33676, '后端菜单'), key: '2', dataParams: {sourcetype: '2'}, conditionDataParams: {sourcetype: '2'}},
                                            {name: getLabel(18773, '自定义菜单'), key: '3', dataParams: {sourcetype: '3'}, conditionDataParams: {sourcetype: '3'}},
                                            {name: getLabel(23094, '系统门户页面'), key: '4', dataParams: {sourcetype: '4'}, conditionDataParams: {sourcetype: '4'}}
                                        ]}
                                        onChange={(id, name, data) => this.setState({link: data.length == '0' ? '' :data[0].fullrouteurl || data[0].linkAddress})}
                                        // onChange={(id, name, data) => this.onChange({'fullrouteurl': data.length == '0' ? '' :data[0].fullrouteurl || data[0].linkAddress, 'linkAddress': data.length == '0' ? '' : data[0].fullrouteurl || data[0].linkAddress || '', 'mobxrouteurl': ''})}
                                    />
                                ) : ''
                                }
                                {linkMode == 'advance2' ? (
                                    <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@u7t655`}
                                        title={getLabel(31560, '系统页面')}
                                        type={'systempage'}
                                        _uuid={'ff56d18b7d98461d808a1f2937a3ba1d'}
                                        isSingle={false}
                                        replaceDatas={replaceDatas}
                                        tabs={[
                                            {name: getLabel(22243, '文档'), key: '1', dataParams: {sourcetype: '1'}},
                                            {name: getLabel(30045, '流程'), key: '2', dataParams: {sourcetype: '2'}},
                                        ]}
                                        onChange={(id, name, data) =>{
                                            if(data.length == 0){
                                                this.setState({link: "",replaceDatas:[]})
                                            }
                                            let menuhref = '';
                                            if(data[0] && data[0].activeKey == '1'){
                                                menuhref = data[0].dropMenuUrl + id;
                                                this.setState({link: menuhref,replaceDatas: data})
                                            }else if(data[0] && data[0].activeKey == '2'){
                                                let arr = id.split(',');
                                                let wftypeIds = [];
                                                let wfIds = [];
                                                arr.map((item) => {
                                                    if(item.lastIndexOf('wftype') != -1){
                                                        wftypeIds.push(item.split('wftype_').join(''));
                                                    }
                                                    // else{
                                                    //     wfIds.push(item.split('wf_').join(''));
                                                    // }
                                                });
                                                data.map(item => {
                                                    if(item.allVersionIds && item.allVersionIds != '') {
                                                        const allVersionIdsArrs = item.allVersionIds.split(',');
                                                        if(allVersionIdsArrs.length > 1) {
                                                            allVersionIdsArrs.map(allVersionIdsItem => {
                                                                if(allVersionIdsItem != '') {
                                                                    wfIds.push(allVersionIdsItem);
                                                                }
                                                            })
                                                        }else {
                                                            wfIds.push(allVersionIdsArrs.join(''));
                                                        }
                                                    }else if(item.allVersionIds == undefined){//回显单个删除时走的逻辑
                                                        if(item.id.lastIndexOf('wftype') == -1){
                                                            wfIds.push(item.id.split('wf_').join(''));
                                                        }
                                                    }
                                                })
                                                let viewType = '';
                                                if(data[0] && data[0].dropMenuValue == '1'){
                                                    let jsonstr = 'wfRange=' + wfIds.toString();
                                                    menuhref = data[0].dropMenuUrl + jsonstr;
                                                    this.setState({link: menuhref,replaceDatas: data});
                                                    return;
                                                }else if(data[0] && data[0].dropMenuValue == '2'){
                                                    viewType = "1";
                                                }else if(data[0] && data[0].dropMenuValue == '3'){
                                                    viewType = "2";
                                                }else if(data[0] && data[0].dropMenuValue == '4'){
                                                    viewType = "3";
                                                }else if(data[0] && data[0].dropMenuValue == '5'){
                                                    viewType = "5";
                                                }else if(data[0] && data[0].dropMenuValue == '6'){
                                                    viewType = "6";
                                                }else if(data[0] && data[0].dropMenuValue == '7'){
                                                    viewType = "4";
                                                }
                                                let jsonstr = {
                                                    flowids:wfIds,
                                                    viewType:viewType,
                                                    typeids:wftypeIds,
                                                    isExclude : 0
                                                };
                                                if(viewType== '2'){
                                                    jsonstr.isArrangement =1;
                                                }else if(viewType == '1'){
                                                    jsonstr.showView =1;
                                                }
                                                menuhref = data[0].dropMenuUrl + escape(JSON.stringify(jsonstr));
                                                this.setState({link: menuhref,replaceDatas: data})
                                            }
                                        }}
                                    />
                                ) : ''
                                }
                            </Col>
                            <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@9b62iw`} span={1}>
                                <div style={{position: 'absolute', top: '6px'}}>
                                    <WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@m5qygf`} title={getLabel('386567', '外部地址，请在地址前加上http://或https://')}/>
                                </div>
                            </Col>
                        </Row>
                    </WeaFormItem>
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@nrd21n`} label={getLabel(83749,'背景色') } labelCol={{span: 6}} wrapperCol={{span: 16}}>
                        <WeaColorPicker ecId={`${this && this.props && this.props.ecId || ''}_WeaColorPicker@w4wdxw`}
                            value={backGroundColor}
                            onChange={(v)=> this.setState({backGroundColor: v})}
                        />
                        <WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@zxgt8o`} style={{position: 'absolute',
                            top: '7px',
                            left: '110px'}} title={getLabel(516344,'仅在显示样式为有背景色时有效')}/>
                    </WeaFormItem>
                </WeaSearchGroup>
            </WeaDialog>

        );
    }

    getButtons() {
        let buttons = [];
        buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mixza3`} type="primary" onClick={this.onOk}>{getLabel(826, '确定')}</Button>);
        buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@f1j4wx`} type="ghost" onClick={this.onCancel}>{getLabel(31129, '取消')}</Button>);
        return buttons;
    }

    onOk() {
        if (!this.state.title) {
            message.warning(getLabel(15859, '必要信息不完整！'));
        } else {
            this.props.onOk(this.state)
        }
    }

    onCancel() {
        this.props.onCancel();
    }
}

export default Content_QuickEntry_DataSource;
